<?php
class scenario_data
{
	//Scenario constants, as stored in SQL
	public $code="INVALID";
	public $title="";
	public $subtitle="";
	public $minlevel=1;
	public $maxlevel=1;
	public $desc="";

}

function oh_fetch_scen_data($code)
{
	$con=oh_sql_connect();
	
	$code=$con->real_escape_string($code);
	
	$scen=new scenario_data();
	
	$sql="SELECT `system`,`title`,`subtitle`,`levelmin`,`levelmax`,`scen_desc` FROM `oh_scenarios` WHERE `scenariocode` = '".$code."'";
		
	$result=oh_sql_query($con,$sql);
		
	if( $result->num_rows > 0)
	{	
		if($row=$result->fetch_assoc())
		{
			$scen->code=$code;
			$scen->system=$row['system'];
			$scen->title=$row['title'];
			$scen->subtitle=$row['subtitle'];
			$scen->minlevel=$row['levelmin'];
			$scen->maxlevel=$row['levelmax'];
			$scen->desc=$row['scen_desc'];
		}
	}
	
	$con->close;
	
	return $scen;	
}

function oh_scen_delete($code)
{
	$con=oh_sql_connect();
	// first, sanitize all input
	
	$code=$con->real_escape_string($code);

	$sql="DELETE FROM `oh_scenarios` WHERE `scenariocode` = '".$code."'";
	
	oh_sql_query($con,$sql);
	
	return "success";
}

function oh_scen_update($code,$system,$title,$subtitle,$minlevel,$maxlevel,$desc)
{
	$con=oh_sql_connect();
	// first, sanitize all input
	
	$code=$con->real_escape_string($code);
	$system=$con->real_escape_string($system);
	$minlevel = filter_var($minlevel, FILTER_SANITIZE_NUMBER_INT);
	$maxlevel = filter_var($maxlevel, FILTER_SANITIZE_NUMBER_INT);
	$title=$con->real_escape_string($title);
	$subtitle=$con->real_escape_string($subtitle);
	$desc=$con->real_escape_string($desc);
	
	$sql = "UPDATE `oh_scenarios` SET
		`system`='".$system."',
		`levelmin`='".$minlevel."',
		`levelmax`='".$maxlevel."',
		`title`='".$title."',
		`subtitle`='".$subtitle."',
		`scen_desc`='".$desc."' WHERE
		`scenariocode`='".$code."'";
		
	oh_sql_query($con,$sql);
	
	return "success";
}

function oh_scen_add($code,$system,$title,$subtitle,$minlevel,$maxlevel,$desc)
{
	$con=oh_sql_connect();
	// first, sanitize all input
	
	$code=$con->real_escape_string($code);
	$system=$con->real_escape_string($system);
	$minlevel = filter_var($minlevel, FILTER_SANITIZE_NUMBER_INT);
	$maxlevel = filter_var($maxlevel, FILTER_SANITIZE_NUMBER_INT);
	$title=$con->real_escape_string($title);
	$subtitle=$con->real_escape_string($subtitle);
	$desc=$con->real_escape_string($desc);
	
	// check that we don't have this one already
	$sql="SELECT `scenario_id` FROM `oh_scenarios` where `scenariocode`='".$code."'";
	
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{
		$con->close();
		return "duplicate";
	}
	
	$sql="INSERT INTO `oh_scenarios` (`scenariocode`,`system`,`title`,`subtitle`,`levelmin`,`levelmax`,`scen_desc`) VALUES ".
		"('".$code."',".
		"'".$system."',".
		"'".$title."',".
		"'".$subtitle."',".
		"'".$minlevel."',".
		"'".$maxlevel."',".
		"'".$desc."')";
			
	oh_sql_query($con,$sql);
	
	$con->close();
	return "success";
}

function oh_render_scen_list()
{
	$con=oh_sql_connect();
	$sql="SELECT `scenariocode`,`system`,`title`,`subtitle`,`levelmin`,`levelmax` FROM `oh_scenarios` ORDER BY `scenariocode`";
	
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{	
		echo "<table>\n";
		while($row=$result->fetch_assoc())
		{
			echo "<tr>\n";
			echo "<td class=\"scentabledata\">".$row['scenariocode']."</td>\n";
			echo "<td class=\"scentabledata\">[".$row['system']."] ";
			if($row['subtitle']!="")
				echo $row['title']."<br \>".$row['subtitle']."</td>\n";
			else
				echo $row['title']."</td>\n";
			if($row['levelmax']==$row['levelmin'])
				echo "<td class=\"scentabledata\">".$row['levelmax']."</td>\n";			
			else
			echo "<td class=\"scentabledata\">".$row['levelmin']."-".$row['levelmax']."</td>\n";			
			echo "<td class=\"scentabledata\">";
			make_button("Edit","scenedit.php?code=".$row['scenariocode']);
			make_button("Delete","scenarios.php?delete=".$row['scenariocode']);
			echo "</td>";
			echo "</tr>\n";
		}		
		echo "</table>\n";
	}
	$con->close();	
}

function oh_render_scen_dropdown()
{
	$con=oh_sql_connect();
	$sql="SELECT `scenariocode`,`system`,`title`,`subtitle`,`levelmin`,`levelmax` FROM `oh_scenarios` ORDER BY `scenariocode`";
	
	$result=oh_sql_query($con,$sql);
	
	echo "<select name=\"scenario\">\n";
	
	if( $result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())
		{
			echo "<option value=\"".$row['scenariocode']."\">".$row['title']."</option>\n";
		}
	}
	
	echo "</select>";
}
?>